// 用户活动相关功能

// 显示我的问题
window.showMyQuestions = function() {
    // 更新标签样式
    document.querySelector('.user-activity-tabs .tab-btn.active').classList.remove('active');
    document.querySelector('.user-activity-tabs .tab-btn:nth-child(1)').classList.add('active');
    
    // 显示问题列表，隐藏回答列表
    document.getElementById('myQuestionsList').style.display = 'block';
    document.getElementById('myAnswersList').style.display = 'none';
    
    // 加载我的问题
    loadMyQuestions();
};

// 加载我的问题
window.loadMyQuestions = function() {
    if (!currentUserId) return;
    
    fetch(`/knowledge-qa/api/question/user/${currentUserId}`)
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                const questions = data.questions || [];
                const questionsList = document.getElementById('myQuestionsList');
                
                if (questions.length === 0) {
                    questionsList.innerHTML = '<p style="text-align:center; color:#999; padding:2rem;">暂无提问</p>';
                } else {
                    let html = '';
                    questions.forEach(question => {
                        html += `
                            <div class="question-item">
                                <div class="question-header">
                                    <h3 onclick="showQuestionDetail(${question.id})" class="question-title">${question.title}</h3>
                                    <div class="question-meta">
                                        <span class="question-time">${formatDate(question.createTime)}</span>
                                        <span class="question-status">${getStatusText(question.status)}</span>
                                    </div>
                                </div>
                                <div class="question-content">
                                    ${truncateText(question.content, 100)}
                                </div>
                                <div class="question-footer">
                                        <span class="question-reward">悬赏: ${question.rewardAmount}金币</span>
                                        <span class="question-view-count">浏览量: ${question.viewCount}</span>
                                        <span class="question-answer-count">回答量: ${question.answerCount}</span>
                                    </div>
                            </div>
                        `;
                    });
                    questionsList.innerHTML = html;
                }
            } else {
                document.getElementById('myQuestionsList').innerHTML = '<p style="text-align:center; color:#ff4d4f; padding:2rem;">加载失败</p>';
            }
        })
        .catch(error => {
            console.error('加载我的问题失败:', error);
            document.getElementById('myQuestionsList').innerHTML = '<p style="text-align:center; color:#ff4d4f; padding:2rem;">加载失败，请稍后重试</p>';
        });
}

// 显示我的回答
function showMyAnswers() {
    // 更新标签样式
    document.querySelector('.user-activity-tabs .tab-btn.active').classList.remove('active');
    document.querySelector('.user-activity-tabs .tab-btn:nth-child(2)').classList.add('active');
    
    // 显示回答列表，隐藏问题列表
    document.getElementById('myQuestionsList').style.display = 'none';
    document.getElementById('myAnswersList').style.display = 'block';
    
    // 加载我的回答
    loadMyAnswers();
};

// 加载我的回答
window.loadMyAnswers = function() {
    if (!currentUserId) return;
    
    fetch(`/knowledge-qa/api/answer/user/${currentUserId}`)
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                const answers = data.answers || [];
                const answersList = document.getElementById('myAnswersList');
                
                if (answers.length === 0) {
                    answersList.innerHTML = '<p style="text-align:center; color:#999; padding:2rem;">暂无回答</p>';
                } else {
                    let html = '';
                    answers.forEach(answer => {
                        const isAccepted = answer.status === 1; // 1表示已采纳
                        
                        html += `
                            <div class="answer-item ${isAccepted ? 'accepted' : ''}">
                                <div class="answer-meta">
                                    <h4 onclick="showQuestionDetail(${answer.questionId})" class="question-title">${answer.questionTitle}</h4>
                                    <span class="answer-time">${formatDate(answer.createTime)}</span>
                                    ${isAccepted ? '<span class="accepted-badge">已采纳</span>' : ''}
                                </div>
                                <div class="answer-content">
                                    ${truncateText(answer.content, 150)}
                                </div>
                                <button class="btn btn-outline" onclick="showQuestionDetail(${answer.questionId})">查看详情</button>
                            </div>
                        `;
                    });
                    answersList.innerHTML = html;
                }
            } else {
                document.getElementById('myAnswersList').innerHTML = '<p style="text-align:center; color:#ff4d4f; padding:2rem;">加载失败</p>';
            }
        })
        .catch(error => {
            console.error('加载我的回答失败:', error);
            document.getElementById('myAnswersList').innerHTML = '<p style="text-align:center; color:#ff4d4f; padding:2rem;">加载失败，请稍后重试</p>';
        });
}

// 加载热门问题
window.loadFeaturedQuestions = function() {
    fetch('/knowledge-qa/api/question/featured?limit=5')
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                const questions = data.questions || [];
                const featuredList = document.getElementById('featuredQuestionList');
                
                if (questions.length === 0) {
                    featuredList.innerHTML = '<p style="text-align:center; color:#999;">暂无热门问题</p>';
                } else {
                    let html = '';
                    questions.forEach(question => {
                        html += `
                            <div class="featured-question-item">
                                <h4 onclick="showQuestionDetail(${question.id})" class="question-title">${question.title}</h4>
                                <div class="featured-question-meta">
                                    <span class="question-user">${question.username}</span>
                                    <span class="question-reward">悬赏: ${question.rewardAmount}金币</span>
                                    <span class="question-view-count">浏览量: ${question.viewCount}</span>
                                    <span class="question-answer-count">回答量: ${question.answerCount}</span>
                                </div>
                            </div>
                        `;
                    });
                    featuredList.innerHTML = html;
                }
            } else {
                document.getElementById('featuredQuestionList').innerHTML = '<p style="text-align:center; color:#999;">加载失败</p>';
            }
        })
        .catch(error => {
            console.error('加载热门问题失败:', error);
        });
}